using UnityEditor;
using UnityEngine;
using SocketWeaver.FixedMath;
using System.IO;

namespace SocketWeaver.FPhysics2D.CustomUnityEditor
{
    public static class FPhysicsMenu
    {
        [MenuItem("GameObject/SocketWeaver/FPhyscis/2D/Physics Controller", false, 0)]
        public static void CreatePhysicsController2D()
        {
            var gameObject = new GameObject("FPhysics2DManager",
                typeof(FPhysics2DManager));

            Selection.activeGameObject = gameObject;
            SceneView.FrameLastActiveSceneView();
        }

        [MenuItem("GameObject/SocketWeaver/FPhyscis/2D/Circle", false, 0)]
        public static void CreateCircle()
        {
            GameObject gameObject = new GameObject("Circle");

            SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>();
            Sprite circle = Resources.Load<Sprite>("FCircle");
            sr.sprite = circle;
            gameObject.AddComponent<FTransform>();
            gameObject.AddComponent<FRigidbody2D>();
            gameObject.AddComponent<FCircleCollider>();

            Selection.activeGameObject = gameObject;
            SceneView.FrameLastActiveSceneView();
        }

        [MenuItem("GameObject/SocketWeaver/FPhyscis/2D/Box", false, 0)]
        public static void CreateBox()
        {
            GameObject gameObject = new GameObject("Box");

            SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>();
            Sprite circle = Resources.Load<Sprite>("FBox");
            sr.sprite = circle;
            gameObject.AddComponent<FTransform>();
            gameObject.AddComponent<FRigidbody2D>();
            gameObject.AddComponent<FBoxCollider2D>();

            Selection.activeGameObject = gameObject;
            SceneView.FrameLastActiveSceneView();
        }

        [MenuItem("GameObject/SocketWeaver/FPhyscis/2D/Capcule", false, 0)]
        public static void CreateCapsule2D()
        {
            GameObject gameObject = new GameObject("Capsule");

            SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>();
            Sprite circle = Resources.Load<Sprite>("FCapsule");
            sr.sprite = circle;
            gameObject.AddComponent<FTransform>();
            gameObject.AddComponent<FRigidbody2D>();
            gameObject.AddComponent<FCapsuleCollider2D>();

            Selection.activeGameObject = gameObject;
            SceneView.FrameLastActiveSceneView();
        }

        [MenuItem("GameObject/SocketWeaver/FPhyscis/2D/Isometric Diamond", false, 0)]
        public static void CreateIsometricDiamond()
        {
            GameObject gameObject = new GameObject("IsometricDiamond");

            SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>();
            Sprite circle = Resources.Load<Sprite>("FIsometricDiamond");
            sr.sprite = circle;
            gameObject.AddComponent<FTransform>();
            gameObject.AddComponent<FRigidbody2D>();
            gameObject.AddComponent<FPolygonCollider2D>();

            Selection.activeGameObject = gameObject;
            SceneView.FrameLastActiveSceneView();
        }

        [MenuItem("GameObject/SocketWeaver/FPhyscis/2D/Hexagon Flat Top", false, 0)]
        public static void CreateHexagonFlatTop()
        {
            GameObject gameObject = new GameObject("HexagonFlatTop");

            SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>();
            Sprite circle = Resources.Load<Sprite>("FHexagonFlatTop");
            sr.sprite = circle;
            gameObject.AddComponent<FTransform>();
            gameObject.AddComponent<FRigidbody2D>();
            gameObject.AddComponent<FPolygonCollider2D>();

            Selection.activeGameObject = gameObject;
            SceneView.FrameLastActiveSceneView();
        }

        [MenuItem("GameObject/SocketWeaver/FPhyscis/2D/Hexagon Pointed Top", false, 0)]
        public static void CreateHexagonPointedTop()
        {
            GameObject gameObject = new GameObject("HexagonPointedTop");

            SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>();
            Sprite circle = Resources.Load<Sprite>("FHexagonPointedTop");
            sr.sprite = circle;
            gameObject.AddComponent<FTransform>();
            gameObject.AddComponent<FRigidbody2D>();
            gameObject.AddComponent<FPolygonCollider2D>();

            Selection.activeGameObject = gameObject;
            SceneView.FrameLastActiveSceneView();
        }
    }


    [InitializeOnLoad]
    static class InstallGizmos
    {
        public static void Copy(string sourceDirectory, string targetDirectory)
        {
            DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
            DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

            CopyAll(diSource, diTarget);
        }

        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            Directory.CreateDirectory(target.FullName);

            // Copy each file into the new directory.
            foreach (FileInfo fi in source.GetFiles())
            {
                if (fi.Name.Contains("meta"))
                {
                    continue;
                }

                string path = Path.Combine(target.FullName, fi.Name);

                //only copy png files
                if (fi.Name.Contains(".png"))
                {
                    Debug.Log($"Copying src={fi.Name}");
                    Debug.Log($"Copying dst={path}");
                    fi.CopyTo(path, true);
                }
            }
        }

        static InstallGizmos()
        {
            string dstFile = Application.dataPath + "/Gizmos";

            if (!Directory.Exists(dstFile))
                Directory.CreateDirectory(dstFile);

            dstFile = Application.dataPath + "/Gizmos/SocketWeaver";

            if (!Directory.Exists(dstFile))
                Directory.CreateDirectory(dstFile);

            dstFile = Application.dataPath + "/Gizmos/SocketWeaver/FPhysics2D";

            if (Directory.Exists(dstFile))
            {
                return;
            }

            Directory.CreateDirectory(dstFile);

            string kPackageRoot = "Packages/com.socketweaver.fphysics2d";
            string path = Path.GetFullPath(kPackageRoot + "/Gizmos/FPhysics2D");
            path = path.Replace('\\', '/'); // because of GetFullPath()
            int index = path.LastIndexOf("/Editor");
            if (index >= 0)
                path = path.Substring(0, index);
            if (path.Length > 0)
                path = Path.GetFullPath(path);  // stupid backslashes

            Copy(path, dstFile);
        }
    }
}